package com.school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.school.domain.Product;
import com.school.extra.base.BaseController;
import com.school.extra.pojo.ProductVo;
import com.school.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;
import zhibi.utils.request.ParamUtils;

import java.text.ParseException;
import java.util.Date;

@Controller
@RequestMapping("product")
public class ProductController extends BaseController {


    @Autowired
    private ProductMapper productMapper;

    /**
     * 列表
     *
     * @param product
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Product product, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = Example.getInstance();
        example.addParam("name", product.getName(), ExampleType.Operation.LIKE);
        //只能看自己的
        if (!sessionUser().getType().equals("admin")) {
            example.addParam("userid", product.getUserid());
        }

        PageHelper.startPage(pageNum, pageSize);
        PageInfo<ProductVo> pageInfo = new PageInfo<ProductVo>(productMapper.selectVoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "product/list";
    }


    /**
     * 家居详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Product product = productMapper.selectByPrimaryKey(id);
        model.addAttribute(product);
        return "product/detail";
    }

    /**
     * 修改家居信息
     *
     * @param product
     * @return
     */
    @RequestMapping("update")
    public String update(Product product, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            product.setImg(saveFile(file));
        }
        productMapper.updateByPrimaryKeySelective(product);
        return refresh();
    }

    /**
     * 删除家居
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        productMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    @GetMapping("add")
    public String add() {
        return "product/add";
    }

    /**
     * 添加家居
     *
     * @param product
     * @return
     */
    @PostMapping("add")
    public String add(Product product, MultipartFile file) throws ParseException {
        String path = saveFile(file);
        product.setImg(path);
        product.setAddtime(new Date());
        product.setUserid(sessionUser().getId());
        productMapper.insert(product);
        return "redirect:list";
    }

}
